//
//  SERegisterPasswordViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/12.
//

import UIKit

class SERegisterPasswordViewController: SEBaseViewController {

    @IBOutlet weak var passwordTextView: SEAuthTextFildView! {
        didSet {
            passwordTextView.viewType = .password
        }
    }
    @IBOutlet weak var confirmPsdTextView: SEAuthTextFildView! {
        didSet {
            confirmPsdTextView.viewType = .confirmPsd
        }
    }

    @IBOutlet weak var confirmBtn: SERegistrationConfirmBtnView! {
        didSet {
            confirmBtn.title = "完成"
            confirmBtn.btnBgColor = UIColor.init(hex: "03C691")
            confirmBtn.delegate = self
        }
    }

    var viewModel = SEAuthViewModel()

    init(with viewModel: SEAuthViewModel) {
        self.viewModel.account = viewModel.account
        self.viewModel.code = viewModel.code
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        addEndEditingTap()
        // Do any additional setup after loading the view.
    }

    override func setupUI() {
        super.setupUI()
        viewModel.updateData = { status in
            switch status {
            case .success:
                SEHUD.show(message: "注册成功,请登录!")
                self.navigationController?.popToRootViewController(animated: true)
            case .auth(let msg):
                SEHUD.showError(message: msg)
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

}

extension SERegisterPasswordViewController: SERegistrationConfirmBtnViewDelegate {
    func registrationConfirmBtnViewAction(view: SERegistrationConfirmBtnView) {
        self.view.endEditing(true)
        register()
    }
}

extension SERegisterPasswordViewController {
    private func register() {
        viewModel.password = passwordTextView.textField.text!
        viewModel.confirmPassword = confirmPsdTextView.textField.text!
        viewModel.register()
    }
}
